package com.ey12.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.ey12.db.dao.GmUserDAO;
import com.ey12.db.domain.GmOrder;
import com.ey12.db.domain.GmUser;
import com.ey12.file.DBPoolFactory;
import com.ey12.utils.DateUtil;
import com.ey12.utils.MD5Util;
import com.ey12.utils.MailUtil;
import com.ey12.utils.Mailtask;

public class GmUsersService {

	private GmUserDAO gmUserDAO;

	public GmUserDAO getGmUserDAO() {
		return gmUserDAO;
	}

	public void setGmUserDAO(GmUserDAO gmUserDAO) {
		this.gmUserDAO = gmUserDAO;
	}
	
	public void sendMail(String receiver,String username){
		System.out.println("sendMail="+username+" send to " +receiver);
		Mailtask task= new Mailtask(receiver,username,1);
		task.start();
		//new MailUtil().sendMail(receiver, username);
	}
	public void sendPayPassword(String receiver,String username){
		System.out.println("sendMail="+username+" send to " +receiver);
		Mailtask task= new Mailtask(receiver,username,2);
		task.start();
		//new MailUtil().sendPayPassword(receiver, username);
	}
	public GmUser findUser(String username) {
		if (username != "") {
			List list = gmUserDAO.findByUserName(username);
			System.out.println(list.size());
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				return user;
			}
		}
		return null;
	}

	public GmUser refreshUser(GmUser paramuser) {
		System.out.println(paramuser.getUserName());
		List list = gmUserDAO.findByUserName(paramuser.getUserName());
		if (list.size() > 0) {
			
			GmUser user = (GmUser) list.get(0);
			System.out.println(user.getPoint().intValue());
			return user;
		}
		return null;
	}

	public String checkuserByEmailAndPA(String username, String protectAnswer,
			String email) {
		if (username != "") {
			List list = gmUserDAO.findByUserName(username);
			System.out.println(list.size());
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				if (user.getProtectAnswer().equals(protectAnswer)
						&& user.getUserEmail().equals(email)) {
					String password = "000000";
					user.setUserPass(MD5Util.toMD5(password));
					gmUserDAO.saveOrUpdate(user);
					return "success";
				}
			}
		}
		return "fail";
	}
	public String checkuserByEmail(String username, String protectAnswer,
			String email) {
		if (username != null) {
		if (username != "") {
			List list = gmUserDAO.findByUserName(username);
			System.out.println(list.size());
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				if(user.getPayProtectAnswer()!=null&&user.getUserEmail()!=null){
				if (user.getPayProtectAnswer().equals(protectAnswer)
						&& user.getUserEmail().equals(email)) {
					String password = "000000";
					user.setPayPass(MD5Util.toMD5(password));
					gmUserDAO.saveOrUpdate(user);
					return "success";
				}
				}
			}
		}
		}
		return "fail";
	}
	public String checkInUser(String username, String protectAnswer){
		if (username != null) {
			if (username != "") {
				List list = gmUserDAO.findByUserName(username);
				if (list.size() > 0) {
					GmUser user = (GmUser) list.get(0);
					if(user.getProtectAnswer()!=null){
					if (user.getProtectAnswer().equals(protectAnswer)) {
						return "success";
					}
					}
				}
			}
			}
			return "fail";
	}
	public String checkPayUser(String username, String protectAnswer){
		if (username != null) {
			if (username != "") {
				List list = gmUserDAO.findByUserName(username);
				if (list.size() > 0) {
					GmUser user = (GmUser) list.get(0);
					if(user.getPayProtectAnswer()!=null){
					if (user.getPayProtectAnswer().equals(protectAnswer)) {
						return "success";
					}
					}
				}
			}
			}
			return "fail";
	}
	public String getQuestion(String username) {
		if (username != "") {
			List list = gmUserDAO.findByUserName(username);
			System.out.println(list.size());
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				return user.getProtectQuestion();
			}
		}
		return null;
	}

	public String getQuestionpay(String username) {
		if (username != "") {
			List list = gmUserDAO.findByUserName(username);
			System.out.println(list.size());
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				return user.getPayProtectQuestion();
			}
		}
		return null;
	}
	public GmUser checkMessege(String username) {
		List list = gmUserDAO.findByUserName(username);
		System.out.println(list.size());
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			return user;
		}
		return null;

	}

	public Double getAllCount() throws Exception {
		try {
			System.out.println("sum=");
			List list = gmUserDAO.findAllCount();
			System.out.println("sum=" + list.size());
			if (list.size() > 0) {
				Double sum = (Double) list.get(0);
				System.out.println("sum=" + sum);
				return sum;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public List getAllMembers() throws Exception {
		try {
			List list = gmUserDAO.findAllMembers();
			if (list.size() > 0) {
				return switchUserValues(list);
			}
			return null;

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	public List getMembersData(String qq, String userName, Integer userType) {
		List list = gmUserDAO.findMembersData(qq, userName, userType);
		if (list.size() > 0) {
			return switchUserValues(list);
		}
		return null;
	}

	public List getUserList() throws Exception {
		try{
			List result = new ArrayList();
			List list = gmUserDAO.findUserList();
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					if (i > 9) {
						break;
					}
					GmUser gm = (GmUser) list.get(i);
					result.add(gm);
				}
			}
			System.out.println("getUserList()=" + result.size());
			return result;
		}catch(Exception re){
			re.printStackTrace();
			throw re;
		}
	}
	
	public Double drawCash(GmUser guser,Double money){
		List list = gmUserDAO.findByUserName(guser.getUserName());
		if(list.size()>0){
			GmUser user = (GmUser) list.get(0);
			user.setCountBalance(user.getCountBalance()-money);
			gmUserDAO.saveOrUpdate(user);
			return user.getCountBalance();
		}
		return null;
	}

	public Set getSonUserList(GmUser guser) {
		List result = new ArrayList();
		List list = gmUserDAO.findByUserName(guser.getUserName());
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			System.out.println("getSonUserList()=" + user.getGmUsers().size());
			return user.getGmUsers();
		}
		// for(int i=0;i<list.size();i++){
		// if(i>9){
		// break;
		// }
		// GmUser gm =(GmUser)list.get(i);
		// result.add(gm);
		// }
		// }
		// System.out.println("getUserList()="+result.size());
		// return result;
		System.out.println("getSonUserList()=" + guser.getGmUsers().size());
		return null;
	}

	public GmUser findUserById(String userId) {
		if (userId != "") {
			List list = gmUserDAO.findByIdCardNum(userId);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				return user;
			}
		}

		return null;
	}

	public GmUser checkAndUpdate(String username, String oldpassword,
			String newpassword, String oldpaypassword, String newpaypassword,
			String newprotectQus, String newprotectAns,String newpayprotectQus, String newpayprotectAns) {
		List list = gmUserDAO.findByUserName(username);
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			if (oldpassword != null && oldpassword != "" && newpassword != null
					&& newpassword != "") {
				if ((user.getUserPass().equals(MD5Util.toMD5(oldpassword)))
						&& (user.getIsValid() == 1)) {
					user.setUserPass(MD5Util.toMD5(newpassword));
				} else {
					return null;
				}
			}
			if (oldpaypassword != null && oldpaypassword != ""
					&& newpaypassword != null && newpaypassword != "") {
				if ((user.getPayPass().equals(MD5Util.toMD5(oldpaypassword)))
						&& (user.getIsValid() == 1)) {
					user.setPayPass(MD5Util.toMD5(newpassword));

				} else {
					return null;
				}
			}
			if (newprotectQus != null && newprotectQus != ""
					&& newprotectAns != null && newprotectAns != "") {
				user.setProtectQuestion(newprotectQus);
				user.setProtectAnswer(newprotectAns);
			}
			if (newpayprotectQus != null && newpayprotectQus != ""
				&& newpayprotectAns != null && newpayprotectAns != "") {
			user.setPayProtectQuestion(newpayprotectQus);
			user.setPayProtectAnswer((newpayprotectAns));
		}
			gmUserDAO.saveOrUpdate(user);
			return user;
		}
		return null;
	}
//	public GmUser checkAndUpdatePayPass(String username, String oldpaypassword, String newpaypassword){
//		List list = gmUserDAO.findByUserName(username);
//		if (list.size() > 0) {
//			GmUser user = (GmUser) list.get(0);
//			if (oldpaypassword != null && oldpaypassword != ""
//				&& newpaypassword != null && newpaypassword != "") {
//			if ((user.getPayPass().equals(MD5Util.toMD5(oldpaypassword)))
//					&& (user.getIsValid() == 1)) {
//				user.setPayPass(MD5Util.toMD5(newpaypassword));
//				
//
//			} else {
//				return null;
//			}
//			gmUserDAO.saveOrUpdate(user);
//			return user;
//			}
//			return null;
//		}
//	}
	public String checkBuyer(GmUser guser, String bankCount, String payPass,
			Double count,int i) throws Exception {
		try {
			List list = gmUserDAO.findByUserName(guser.getUserName());
			System.out.println(list.size()+"--"+payPass+"--"+count+"--"+i);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				//basin--3333333333333333333--1--1231212412312311111--3333333333333333333
				System.out.println(user.getUserName()+"--"+user.getPayPass()+"--"+MD5Util.toMD5(payPass)+"--"+bankCount+"--"+i+"--"+user.getZfbCountNum()+"--"+user.getBankAccount());
				if (user.getPayPass() != null ) {
					if(!"".equals(user.getPayPass())){
					if ((user.getPayPass().equals(MD5Util.toMD5(payPass)))
							&& (user.getIsValid() == 1)&& (user.getCountBalance() >= count)) {
						// System.out.println(username + "valid " +
						// user.getUserName());&& if(user.getBankAccount().equals(bankCount))
						if(i==1){
							if(user.getZfbCountNum()!=null){
								if(user.getZfbCountNum().equals(bankCount)){
									return "success";
								}
							}
							return null;
						}
						else if(i==2){
							if(user.getBankAccount()!=null){
								if(user.getBankAccount().equals(bankCount)){
									return "success";
								}
							}
							return null;
						}
//						return "success";
					} else {
						// System.out.println(username + "not valid " +
						// user.getUserName());
						return null;
					}
				}
				}
				return null;
			}
			return null;
		} catch (Exception re) {
			re.printStackTrace();
			throw re;
		}

	}

	public String checkPaypass(GmUser user, String payPass) {
		try {
			System.out.println("--" + payPass);
			if (user.getPayPass() != null) {
				if (payPass != null) {
					if ((user.getPayPass().equals(MD5Util.toMD5(payPass)))
							&& (user.getIsValid() == 1)) {
						System.out.println("--success" + payPass);
						return "success";
					}
				}
			}
			return "fail";
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}

	public void findAndUpdateUserByPass(String username, String newpassword) {
		if (newpassword != "") {
			List list = gmUserDAO.findByUserName(username);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				user.setUserPass(MD5Util.toMD5(newpassword));
				gmUserDAO.saveOrUpdate(user);
				// gmUserDAO.update(user);
			}
		}
	}

	public void findAndUpdateUserBypayPass(String username, String newpayPass) {
		List list = gmUserDAO.findByUserName(username);
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			user.setPayPass(MD5Util.toMD5(newpayPass));
			gmUserDAO.saveOrUpdate(user);
		}

	}

	public void findAndUpdateUserByUsername(String username, String newusername) {
		if (newusername != "") {
			List list = gmUserDAO.findByUserName(username);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				user.setUserName(newusername);
				gmUserDAO.saveOrUpdate(user);
			}
		}
	}

	public void findAndUpdateUserByUserEmail(String username, String newemail) {
		if (newemail != "") {
			List list = gmUserDAO.findByUserName(username);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				user.setUserEmail(newemail);
				gmUserDAO.saveOrUpdate(user);
			}
		}
	}

	public void findAndUpdateUserBymobile(String username, String newphonum) {

		if (newphonum != "") {
			List list = gmUserDAO.findByUserName(username);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				user.setMobile(newphonum);
				gmUserDAO.saveOrUpdate(user);
			}
		}
	}

	public void findAndUpdateUserByqqNum(String username, String newqqNum) {
		if (newqqNum != "") {
			List list = gmUserDAO.findByUserName(username);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				user.setQqNum(newqqNum);
				gmUserDAO.saveOrUpdate(user);
			}
		}
	}

	public void findAndUpdateUserBymsnNum(String username, String newmsnNum) {
		if (newmsnNum != "") {
			List list = gmUserDAO.findByUserName(username);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				user.setMsnNum(newmsnNum);
				gmUserDAO.saveOrUpdate(user);
			}
		}
	}

	public void findAndUpdateUserBybankAccount(String username,
			String newbankAccount) {
		if (newbankAccount != "") {
			List list = gmUserDAO.findByUserName(username);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				user.setBankAccount(newbankAccount);
				gmUserDAO.saveOrUpdate(user);
			}
		}
	}

	public void findAndUpdateUserByprotectQuestion(String username,
			String protectQuestion) {
		if (protectQuestion != "") {
			List list = gmUserDAO.findByUserName(username);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				user.setProtectQuestion(protectQuestion);
				gmUserDAO.saveOrUpdate(user);
			}
		}
	}

	public void findAndUpdateUserByprotectAnswer(String username,
			String protectAnswer) {
		if (protectAnswer != "") {
			List list = gmUserDAO.findByUserName(username);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				user.setProtectAnswer(protectAnswer);
				gmUserDAO.saveOrUpdate(user);
			}
		}
	}

	public void findAndUpdateUserByPayType(String username, String payType) {
		if (payType != "") {
			List list = gmUserDAO.findByUserName(username);
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				user.setBankAccount(payType);
				gmUserDAO.saveOrUpdate(user);
			}
		}
	}

	public GmUser checkUser(String username, String password) throws Exception {
		try {
			List list = gmUserDAO.findByUserName(username);
			System.out.println(list.size());
			if (list.size() > 0) {
				GmUser user = (GmUser) list.get(0);
				System.out.println(username + " " + user.getUserName());
				if ((user.getUserPass().equals(MD5Util.toMD5(password)))
						&& (user.getIsValid() == 1)) {
					user.setIsOnline(new Integer(1));
					System.out
							.println(username + "valid " + user.getUserName());
					gmUserDAO.saveOrUpdate(user);
					System.out.println(" sellLevelNum="
							+ user.getSellLevelNum());
					return switchUserValues(user);
				} else {
					System.out.println(username + "not valid "
							+ user.getUserName());
					return null;
				}
			}
			return null;
		} catch (Exception re) {
			re.printStackTrace();
			throw re;
		}

	}

	public GmUser checkManager(String username, String password) {
		List list = gmUserDAO.findByUserName(username);
		System.out.println(list.size());
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			System.out.println(username + " " + user.getUserName());
			if (user.getUserType() == 11 || user.getUserType() == 12
					|| user.getUserType() == 13) {
				if ((user.getUserPass().equals(MD5Util.toMD5(password)))
						&& (user.getIsValid() == 1)) {
					return user;
				} else {
					return null;
				}
			}
		}
		return null;
	}

	public GmUser checkAndUpdatePass(String username, String password,
			String newpassword) {
		List list = gmUserDAO.findByUserName(username);
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			if ((user.getUserPass().equals(MD5Util.toMD5(password)))
					&& (user.getIsValid() == 1)) {
				user.setUserPass(MD5Util.toMD5(newpassword));
				gmUserDAO.saveOrUpdate(user);
				return user;
			} else {
				return null;
			}
		}
		return null;
	}

	public GmUser checkAndUpdatePayPass(String username, String password,
			String newpassword) {
		List list = gmUserDAO.findByUserName(username);
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			if ((user.getPayPass().equals(MD5Util.toMD5(password)))
					&& (user.getIsValid() == 1)) {
				user.setPayPass(MD5Util.toMD5(newpassword));
				gmUserDAO.saveOrUpdate(user);
				return user;
			} else {
				return null;
			}
		}
		return null;
	}
	
	public GmUser checkAndUpdatePassPro(String username,String newprotectQus, String newprotectAns) {
		List list = gmUserDAO.findByUserName(username);
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			if ((user.getProtectQuestion()!=null)&&user.getProtectAnswer()!=null
					&& (user.getIsValid() == 1)) {
				user.setProtectQuestion(newprotectQus);
				user.setProtectAnswer(newprotectAns);
				gmUserDAO.saveOrUpdate(user);
				return user;
			} else {
				return null;
			}
		}
		return null;
	}
	public GmUser checkAndUpdatePayPassPro(String username,String newpayprotectQus, String newpayprotectAns) {
		List list = gmUserDAO.findByUserName(username);
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
//			if ((user.getPayProtectQuestion()!=null)&&user.getPayProtectAnswer()!=null
//					&& (user.getIsValid() == 1)) {
				user.setPayProtectQuestion(newpayprotectQus);
				user.setPayProtectAnswer(newpayprotectAns);
				gmUserDAO.saveOrUpdate(user);
				return user;
//			} else {
//				return null;
//			}
		}
		return null;
	}

	public GmUser addUser(String username, String password,
			String userEmail, String mobile, String qqNum,
			String ProQus, String ProAns) {
		System.out.println(username + " " + password);
		if (findUser(username) != null) {
			return null;
		}
		System.out.println(username + " " + password);
		GmUser u = new GmUser();
		u.setUserPass(MD5Util.toMD5(password));
		u.setPayPass(null);
		u.setUserName(username);
		u.setUserEmail(userEmail);
		u.setMobile(mobile);
		u.setQqNum(qqNum);
		u.setProtectQuestion(ProQus);
		u.setProtectAnswer(ProAns);
		u.setPoint(new Integer(0));
		u.setLevelNum(new Integer(1));
		u.setSellLevelNum(new Integer(0));
		u.setRegDate(new DateUtil().toTimeString());
		u.setUserType(new Integer(1));
		u.setIsOnline(new Integer(1));
		u.setCountBalance(new Double(0));
		u.setIsMusic(new Integer(1));
		u.setBuyBargain(new Integer(0));
		u.setDeposit(new Double(0));
		u.setBuyBargainrate(new Integer(0));
		u.setBuyerMessagecount(new Integer(0));
		u.setSaleBargain(new Integer(0));
		u.setSaleBargainrate(new Integer(0));
		u.setSaleBargainrate(new Integer(0));
		u.setSalerMessagecount(new Integer(0));
		u.setIsBalanceValid(new Integer(1));
		u.setIsToBuy(new Integer(1));
		u.setIsValid(new Integer(1));
		gmUserDAO.saveOrUpdate(u);
		return u;
	}
         		
	
	public GmUser addSonUser(GmUser user, String username, String idNum,
			String password, String userEmail, String mobile, String qqNum,
			String ProQus, String ProAns) {
		if ((findUser(username) != null) || (findUserById(idNum) != null)) {
			return null;
		}
		System.out.println(username + " " + password);
		GmUser u = new GmUser();
		u.setUserPass(MD5Util.toMD5(password));
		u.setPayPass(null);
		u.setUserName(username);
		u.setIdCardNum(idNum);
		u.setUserEmail(userEmail);
		u.setMobile(mobile);
		u.setQqNum(qqNum);
		u.setProtectQuestion(ProQus);
		u.setProtectAnswer(ProAns);
		u.setPoint(new Integer(0));
		u.setLevelNum(user.getLevelNum());
		u.setSellLevelNum(user.getSellLevelNum());
		u.setRegDate(new DateUtil().toTimeString());
		u.setUserType(new Integer(2));
		u.setIsOnline(new Integer(1));
		u.setCountBalance(new Double(0));
		u.setIsMusic(new Integer(1));
		u.setBuyBargain(new Integer(0));
		u.setDeposit(new Double(0));
		u.setBuyBargainrate(new Integer(0));
		u.setBuyerMessagecount(new Integer(0));
		u.setSaleBargain(new Integer(0));
		u.setSaleBargainrate(new Integer(0));
		u.setSalerMessagecount(new Integer(0));
		u.setIsBalanceValid(new Integer(1));
		u.setIsToBuy(new Integer(1));
		u.setIsValid(new Integer(1));
		u.setGmUser(user);
		gmUserDAO.saveOrUpdate(u);

		return u;
	}

	public GmUser saveUserState(GmUser guser) {
		System.out.println(guser.getBuyBargainrate());
		System.out.println(guser.getProtectQuestion());
		System.out.println(guser.getProtectAnswer());
		System.out.println(guser.getSalerMessagecount());
		System.out.println("saveUserState="+guser.getCountBalance()+"-"+guser.getDeposit());
		gmUserDAO.saveOrUpdate(guser);
		return guser;
	}
	public void saveUserPoint(GmUser temp) {
		GmUser user=gmUserDAO.findById(temp.getId());
		System.out.println("saveUserPoint="+temp.getId()+" "+temp.getPoint());
		if(user!=null){
			user.setPoint(temp.getPoint());
			gmUserDAO.saveOrUpdate(user);
		}
		
	}
	public void saveUserCountBalance(GmUser temp) {
		GmUser user=gmUserDAO.findById(temp.getId());
		System.out.println("saveUserCountBalance="+temp.getId()+" "+temp.getCountBalance());
		if(user!=null){
			user.setCountBalance(temp.getCountBalance());
			gmUserDAO.saveOrUpdate(user);
		}
	}
	public void saveUserBuyBargain(GmUser temp) {
		GmUser user=gmUserDAO.findById(temp.getId());
		System.out.println("saveUserBuyBargain="+temp.getId()+" "+temp.getBuyBargain());
		if(user!=null){
			user.setBuyBargain(temp.getBuyBargain());
			user.setBuyBargainrate(temp.getBuyBargainrate());
			gmUserDAO.saveOrUpdate(user);
		}
	}
	public void saveUserSaleBargain(GmUser temp) {
		GmUser user=gmUserDAO.findById(temp.getId());
		System.out.println("saveUserSaleBargain="+temp.getId()+" "+temp.getSaleBargain());
		if(user!=null){
			
			user.setSaleBargain(temp.getSaleBargain());
			user.setSaleBargainrate(temp.getSaleBargainrate());
			gmUserDAO.saveOrUpdate(user);
		}
	}
	public void saveState(GmUser guser) {
		System.out.println(guser.getIdCardNum()+guser.getUserType()+"--"+guser.getUserName()+"--"+guser.getUserPass()+"-"+guser.getMobile()+"-"+guser.getMobile()+"--"+guser.getRegDate()+"---"+guser.getCountBalance());
		System.out.println(guser.getProtectQuestion());
		System.out.println(guser.getProtectAnswer());
		System.out.println(guser.getSalerMessagecount());
		gmUserDAO.saveOrUpdate(guser);
	}
	public GmUser savepaypassState(GmUser guser) {
		System.out.println(guser.getBuyBargainrate());
		System.out.println(guser.getProtectQuestion());
		System.out.println(guser.getProtectAnswer());
		System.out.println(guser.getSalerMessagecount());
		System.out.println(guser.getPayProtectAnswer());
		System.out.println(guser.getPayProtectQuestion());
		guser.setPayPass(MD5Util.toMD5(guser.getPayPass()));
		gmUserDAO.saveOrUpdate(guser);
		return guser;
	}
	public GmUser refreshusers(String username){
		List list = gmUserDAO.findByUserName(username);
		if(list.size()>0){
			GmUser user = (GmUser)list.get(0);
			return switchUserValues(user);
		}
		return null;
	}

	public void updateUserInfo(GmUser puser) {
		System.out.println(puser.getBankAccount());
		List list = gmUserDAO.findByUserName(puser.getUserName());
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			user.setUserType(1);
			user.setPoint(0);
			user.setLevelNum(1);
			user.setIsValid(1);
			user.setCountInfo(puser.getCountInfo());
			user.setPaytype(puser.getPaytype());
			user.setBankAccount(puser.getBankAccount());
			user.setUserEmail(puser.getUserEmail());
			user.setMobile(puser.getMobile());
			user.setQqNum(puser.getQqNum());
			user.setPayPass(MD5Util.toMD5(puser.getPayPass()));
			gmUserDAO.saveOrUpdate(user);
		}
	}

	public void changeGrade(GmUser guser, Integer moneyCount) {
		System.out.println("changeGrade to be used");
		List list = gmUserDAO.findByUserName(guser.getUserName());
		if (list.size() > 0) {
			System.out.println("changeGrade using 1");
			GmUser user = (GmUser) list.get(0);
			user.setPoint(moneyCount);
			System.out.println("changeGrade using 2");
			gmUserDAO.saveOrUpdate(user);
			System.out.println("changeGrade used");
		}
	}
	public static Connection getConnectionByJDBC() {		
		return DBPoolFactory.getConnectionByJDBC();
	}
	public void updateUserCountBalance(int user_id,double countBalance) {
		
		String sqlx = "update gm_user set count_balance=count_balance+"+countBalance+" where id="+user_id;
		
		try {
			Connection conn = getConnectionByJDBC();
			Statement stmt = conn.createStatement();
			stmt.execute(sqlx);
			stmt.close();
			conn.close();
			System.out.println("giveBackMoney updateUserCountBalance="+sqlx);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		} 

	}
	public double findUserCountBalance(int user_id) {
		double id = -1;		
		String sqlx = "select count_balance from gm_user where id="+user_id;
		try {
			Connection conn = getConnectionByJDBC();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sqlx);
			if (rs.next()) {
				id = rs.getDouble("count_balance");
			}
			rs.close();
			stmt.close();
			conn.close();
			System.out.println("giveBackMoney findUserCountBalance="+id+"--"+sqlx);
		} catch (SQLException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return id;
	}
	public String giveBackMoney(GmUser guser, Double moneyCount) {
		System.out.println("giveBackMoney to be used");
		updateUserCountBalance(guser.getId(),moneyCount);
		double count_balance=findUserCountBalance(guser.getId());
		if(count_balance!=-1){
			return "用戶:"+guser.getUserName()+",原余额:"+guser.getCountBalance()+",退款:"+moneyCount+",现余额:"+String.valueOf(count_balance);
		}
//		List list = gmUserDAO.findByUserName(guser.getUserName());
//		if (list.size() > 0) {
//			GmUser user = (GmUser) list.get(0);
//			user.setCountBalance(user.getCountBalance()+moneyCount);
//			gmUserDAO.saveOrUpdate(user);
//			System.out.println("giveBackMoney to be used="+guser.getId()+"--"+guser.getUserName()+"--"+moneyCount);
//		}
		return "done";
	}
	
	public void updateUser(String username, String password, String userEmail,
			String mobile, String qqNum, String msnNum) {
		List list = gmUserDAO.findByUserName(username);
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			user.setUserType(1);
			user.setUserPass(MD5Util.toMD5(password));
			user.setPoint(new Integer(0));
			user.setLevelNum(new Integer(1));
			user.setIsValid(new Integer(1));
			user.setUserEmail(userEmail);
			user.setMobile(mobile);
			user.setQqNum(qqNum);
			user.setMsnNum(msnNum);

			gmUserDAO.saveOrUpdate(user);
		}
	}

	public List getTheMember(String qqNum, String username) {
		List list = gmUserDAO.findTheMember(qqNum, username);
		if (list.size() > 0) {
			return switchUserValues(list);
		}
		return null;
	}

	public List showUncheckUser() {
		List list = gmUserDAO.findUncheckUser();
		if (list.size() > 0) {
			return switchUserValues(list);
		}
		return null;
	}

	public void deleteUser(String username) {
		List list = gmUserDAO.findByUserName(username);
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			gmUserDAO.delete(user);
		}
	}

	public void delUser(GmUser guser) {
		List list = gmUserDAO.findByUserName(guser.getUserName());
		if (list.size() > 0) {
			GmUser user = (GmUser) list.get(0);
			gmUserDAO.delete(user);
			System.out.println("delUser used");
		}
	}

	private List switchUserValues(List list) {
		List result = new ArrayList();
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				GmUser gm = (GmUser) list.get(i);
				if (gm.getLevelNum() != null) {
					switch (gm.getLevelNum().intValue()) {
					// 1普通买家2VIP买家3经销商
					case 1:
						gm.setLevelNumValue("普通买家");
						break;
					case 2:
						gm.setLevelNumValue("VIP买家");
						break;
					case 3:
						gm.setLevelNumValue("经销商");
						break;
					}
				}
				if (gm.getSellLevelNum() != null) {
					switch (gm.getSellLevelNum().intValue()) {
					// 4普通卖家5金牌卖家6钻石卖家7内部卖家
					case 4:
						gm.setSellLevelNumValue("普通卖家");
						break;
					case 5:
						gm.setSellLevelNumValue("金牌卖家");
						break;
					case 6:
						gm.setSellLevelNumValue("钻石卖家");
						break;
					case 7:
						gm.setSellLevelNumValue("内部卖家");
						break;
					}
				}
				if (gm.getUserType() != null) {
					switch (gm.getUserType().intValue()) {
					// 1买家2卖家���
					case 1:
						gm.setUserTypeValue("买家");
						break;
					case 2:
						gm.setUserTypeValue("卖家");
						break;
					}
				}
				if (gm.getPaytype() != null) {
					switch (gm.getPaytype().intValue()) {
					// 1支付宝2财付通3网站余额��վ���
					case 1:
						gm.setPaytypeValue("支付宝");
						break;
					case 2:
						gm.setPaytypeValue("财付通");
						break;
					case 3:
						gm.setPaytypeValue("网站余额");
						break;
					}
				}
				result.add(gm);
				// gm = null;
			}
		}

		return result;
	}

	private GmUser switchUserValues(GmUser gm) {
		// List result = new ArrayList();
		// if (list.size() > 0) {
		// for (int i = 0; i < list.size(); i++) {
		// GmUser gm = (GmUser) list.get(i);
		if (gm.getLevelNum() != null) {
			switch (gm.getLevelNum().intValue()) {
			// 1普通买家2VIP买家3经销商
			case 1:
				gm.setLevelNumValue("普通买家");
				break;
			case 2:
				gm.setLevelNumValue("VIP买家");
				break;
			case 3:
				gm.setLevelNumValue("经销商");
				break;
			}
		}
		if (gm.getSellLevelNum() != null) {
			switch (gm.getSellLevelNum().intValue()) {
			// 4普通卖家5金牌卖家6钻石卖家7内部卖家
			case 4:
				gm.setSellLevelNumValue("普通卖家");
				break;
			case 5:
				gm.setSellLevelNumValue("金牌卖家");
				break;
			case 6:
				gm.setSellLevelNumValue("钻石卖家");
				break;
			case 7:
				gm.setSellLevelNumValue("内部卖家");
				break;
			}
		}
		if (gm.getUserType() != null) {
			switch (gm.getUserType().intValue()) {
			// 1买家2卖家���
			case 1:
				gm.setUserTypeValue("买家");
				break;
			case 2:
				gm.setUserTypeValue("卖家");
				break;
			}
		}
		if (gm.getPaytype() != null) {
			switch (gm.getPaytype().intValue()) {
			// 1支付宝2财付通3网站余额��վ���
			case 1:
				gm.setPaytypeValue("支付宝");
				break;
			case 2:
				gm.setPaytypeValue("财付通");
				break;
			case 3:
				gm.setPaytypeValue("网站余额");
				break;
			}
		}
		// result.add(gm);
		// guser = null;
		// }
		// }

		return gm;
	}

}
